package com.lixia.lxframework.framework.web;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

import static com.lixia.lxframework.framework.system.constant.MsgConstants.FAILED_CODE;
import static com.lixia.lxframework.framework.system.constant.MsgConstants.SUCCESS_MSG;

/**
 * 业务类返回值
 *
 * @author Administrator
 * @date 2019/2/22 0:05
 */
@Data
@ApiModel(value = "响应消息实体", description = "返回响应数据")
public class R implements Serializable {
    private static final long serialVersionUID = 8361764392994033644L;
    @ApiModelProperty(value = "是否成功")
    private boolean success = false;
    @ApiModelProperty(value = "返回信息")
    private String message;
    @ApiModelProperty(value = "返回对象")
    private Object data;
    @ApiModelProperty(value = "错误编码")
    private Integer errCode;
    @ApiModelProperty(value = "当前时间戳")
    private Long stamp;

    public R() {
    }

    public R(boolean success, String message, Integer errCode, Object data, Long stamp) {
        this.success = success;
        this.message = message;
        this.data = data;
        this.errCode = errCode;
        this.stamp = stamp;
    }

    public static R success() {
        return success(SUCCESS_MSG, null);
    }

    public static R success(Object data) {
        return success(null, data);
    }

    public static R success(String message) {
        return success(message, null);
    }

    public static R success(String message, Object data) {
        return success(message, data, System.currentTimeMillis());
    }

    public static R success(String message, Object data, Long stamp) {
        return new R(true, message, null, data, stamp);
    }


    public static R failed(String message) {
        return failed(FAILED_CODE, message, null);
    }

    public static R failed(Integer errCode, String message) {
        return failed(errCode, message, null);
    }

    public static R failed(Integer errCode, String message, Object data) {
        return failed(errCode, message, data, System.currentTimeMillis());
    }

    public static R failed(Integer errCode, String message, Object data, Long stamp) {
        return new R(false, message, errCode, data, stamp);
    }
}
